﻿using MES_SystemUI.Domain;
using NPOI.POIFS.NIO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MES_SystemUI.UI
{
    public partial class ProductsPutin : MES_SystemUI.UI.Putin
    {
        private string pathStr = string.Empty;
        public ProductsPutin()
        {
            InitializeComponent();
            button2.Click += Button2_Click;
            textBox3.Click += TextBox3_Click;
            this.Load += ProductsPutin_Load;
        }

        private void ProductsPutin_Load(object sender, EventArgs e)
        {
            ComboBoxBingData();
            RefreshText();
        }

        // 选择框 绑定选项数据
        private void ComboBoxBingData()
        {
            using (var ct = new Context.MES_SystemContext())
            {
                comboBox1.DataSource =ct.users.ToList();
                comboBox1.DisplayMember = "UserName";
                comboBox1.ValueMember = "UserId";
                comboBox2.DataSource =ct.productSpecs.ToList();
                comboBox2.DisplayMember = "ProductSpecName";
                comboBox2.ValueMember = "ProductSpecId";
                comboBox3.DataSource = ct.productCategories.ToList();
                comboBox3.DisplayMember = "ProductCategoryName";
                comboBox3.ValueMember = "ProductCategoryId";
                comboBox4.DataSource =ct.units.ToList();
                comboBox4.DisplayMember = "UnitName";
                comboBox4.ValueMember = "UnitId";
            }
        }

        private void TextBox3_Click(object sender, EventArgs e)
        {
            textBox3.Text = "CPBM" + DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        // 添加图片
        private void Button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "图片(*.jpg,*.png)|*.jpg;*.png";
            ofd.Multiselect = false;
            ofd.RestoreDirectory = true;
            ofd.Title = "选择产品图片";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                pathStr = ofd.FileName;
                pictureBox1.Image = Image.FromFile(pathStr);
            }else
            {
                pathStr = string.Empty;
            }
        }

        public override void PutinData()
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()))
            {
                MessageBox.Show("产品名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(comboBox1.Text.Trim()))
            {
                MessageBox.Show("产品维护人不能为空");
                return;
            }
            if (string.IsNullOrEmpty(comboBox3.Text.Trim()))
            {
                MessageBox.Show("产品类型不能为空");
                return;
            }
            if (string.IsNullOrEmpty(comboBox2.Text.Trim()))
            {
                MessageBox.Show("规格型号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(comboBox4.Text.Trim()))
            {
                MessageBox.Show("单位不能为空");
                return;
            }
            if (string.IsNullOrEmpty(textBox3.Text.Trim()))
            {
                MessageBox.Show("产品编码不能为空");
                return;
            }
            if(MessageBox.Show("是否添加") != DialogResult.OK)
            {
                return;
            }
            using (var ct = new Context.MES_SystemContext())
            {
                
                Domain.Product Modes = new Domain.Product()
                {
                    ProductCode = textBox3.Text,
                    ProductName = textBox1.Text,
                    MaintainerId = int.Parse(comboBox1.SelectedValue.ToString()),
                    MaintenanceTime = dateTimePicker1.Value,
                    ProductCategotyId = int.Parse(comboBox3.SelectedValue.ToString()),
                    ProductSpecId = int.Parse(comboBox2.SelectedValue.ToString()),
                    ProductUnitId = int.Parse(comboBox4.SelectedValue.ToString()),
                    CreateUserId = SystemDataConfig.LoginUser.UserId,
                };
                if (!string.IsNullOrEmpty(pathStr))
                {
                    Modes.ProductImage = MES_SystemUI.Helpers.ImageHelper.ImageToByteArray(Image.FromFile(pathStr));
                }
                ct.Entry(Modes).State = System.Data.Entity.EntityState.Added;
                ct.SaveChanges();
            }
            RefreshText();
        }

        private void RefreshText()
        {
            textBox1.Text = string.Empty;
            comboBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
            comboBox3.SelectedIndex = -1;
            comboBox4.SelectedIndex = -1;
            comboBox1.Text = string.Empty;
            comboBox2.Text = string.Empty;
            comboBox3.Text = string.Empty;
            comboBox4.Text = string.Empty;
            textBox3.Text = string.Empty;
            // 设置dateTimePicker1的当前值
            dateTimePicker1.Value = DateTime.Now;
            // 刷新显示的文本（如果需要）
            dateTimePicker1.Refresh();
        }
    }
}
